open Printf
open Ext

let _ = Random.self_init ()

(* Globals used for command line parameters *)
let bnfile = ref ""
let ofile = ref ""
let numsamples = ref 1000 

let usage = "Usage: bnsample -m <bn> [-o <file>] [...]"
let args = Arg.align
 ([("-m", Arg.Set_string bnfile, " Input Bayes net (BIF or XMOD format)") ;
   ("-o", Arg.Set_string ofile, " Output file");
   ("-n", Arg.Set_int numsamples, " Number of samples to generate [1000]") ;
   ("-seed", Arg.Int Random.init, " Random seed")]
  @ common_arguments)

let _ =
  Arg.parse args ignore usage;
  if !bnfile = "" then
    (Arg.usage args usage; exit 0);
  common_log_init ();
  let bn = Bn.load_auto !bnfile in 
  let ostream =
    if !ofile <> "" then open_out !ofile else stdout in
  for i = 1 to !numsamples do
    Data.output ostream (Bn.sample bn)
  done
